package com.lambda.use;

import org.junit.Test;

import java.util.function.Function;

/**
 * 功能描述
 *
 * @author: jichao.huang
 * @date: 2025年06月04日 10:07
 */
public class FunctionUseTest {

    @Test
    public void testApply(){
        // 定义一个函数，输入一个整数，返回一个整数
        Function<Integer, Integer> function = (x) -> x * x;
        // 调用函数，传入参数5，返回结果25
        int result = function.apply(5);
        System.out.println(result);
    }

    @Test
    public void testCompose(){
        // 定义一个函数，输入一个整数，返回一个整数
        Function<Integer, Integer> function1 = (x) -> x * x;
        Function<Integer, Integer> function2 = (x) -> x +1;
        Function<Integer, Integer> function3 = function1.compose(function2);
        int result = function3.apply(5);
        System.out.println(result);
    }

    @Test
    public void testCompose2(){
        Function<Integer, String> intToString = i -> "Number: " + i;
        Function<String, Integer> stringLength = s -> s.length();

        // 先执行 stringLength，再执行 intToString
        Function<String, String> composed = intToString.compose(stringLength);

        System.out.println(composed.apply("Hello"));  // 输出: Number: 5
    }

    @Test
    public void testAndThen(){
        // 定义一个函数，输入一个整数，返回一个整数
        Function<Integer, Integer> function1 = (x) -> x * x;
        Function<Integer, Integer> function2 = (x) -> x +1;
        Function<Integer, Integer> function3 = function1.andThen(function2);
        int result = function3.apply(5);
        System.out.println(result);
    }

    @Test
    public void testIdentity(){
        // 定义一个函数，输入一个整数，返回一个整数
        //Function<Integer, Integer>  function = Function.identity();
        Function<String, String>  function = Function.identity();


        //System.out.println(function.apply(2));
        System.out.println(function.apply("hello"));
    }

    @Test
    public void testProp(){
        // 定义一个函数，输入一个整数，返回一个整数
        Function<UserInfo, Integer> function = UserInfo::getAge;
        // 调用函数，传入参数5，返回结果25
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(18);
        int result = function.apply(userInfo);
        System.out.println(result);
    }

    public static <T,R,V> void test(Function<T,R> function){
        System.out.println(function.apply(null));
    }
}
